<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Model_Voucher extends Zend_Db_Table{
	
	protected $_name    = "vouchers";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            if(isset($data['id']) && $this->checkExists($data['id'])){
                $id = $data['id'];
                
                $db->update("", $data, "id = $id");
            }else{
                $db->insert('', $data);
            }
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getDatas($options = null, $cols = array()){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        if(count($cols) == 0){
            $cols = array('id', 'category', 'description', 'status');
        }
        
        $select = $db->select()
                    ->from(array('' => ''), $cols);  
                       
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        if(($options != null && !array_key_exists('order', $options)) || $options == null){
            $select->order('');
        }
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($id){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $select = $db->select()
                        ->from(array('' => ''), array('', '', ''));
            
            $rs = $db->fetchRow($select);
            
            return $rs;
        }
        
        return false;
    }
    
    public function checkVoucher($code){
    	if($this->checkVoucherExists($code)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $date = new Zend_Date();
            $curDate = $date->get("Y-M-d");
            
            $select = $db->select()
                        ->from(array('v' => 'vouchers'), array('id', 'voucher_code', 'description', 'order_value', 'value', 'type', 'used'))
                        ->where('voucher_code = ?', $code)
                        ->where('v.quantity > v.used')
                        ->where('? >= v.start_date', $curDate)
                        ->where('v.end_date >= ?', $curDate)
                        ->where('status = 1');
            
            $rs = $db->fetchRow($select);
            
            if(count($rs) == 0){
                return false;
            }
            
            return $rs;
    	}
        
        return false;
    }
    
    public function checkExists($id){
        $option = array('table' => '', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
    
    public function checkVoucherExists($code){
        $option = array('table' => 'vouchers', 'field' => 'voucher_code');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($code)){
            return true;
        }
        
        return false;
    }
}